<?php

namespace app\model;

use think\facade\Db;
use think\Model;

class UserAddressesModel extends Model {

    protected $table ="user_addresses";

    protected $pk = 'address_id';

    private $cacheKey="user:addresses:id:";

    private $cacheTime=5184000;

    /*
   * 获取total
   * */
    public function getTotal($where=[]){
        return Db::name('user_addresses')->where($where)->count();
    }


    /*
     * 获取list
     * */
    public function getList($where=[],$page = null,$limit = null,$orderField = null,$orderType = "ASC"){

        $obj =  Db::name('user_addresses')->field("address_id")->where($where);


        if(!empty($page) && !empty($limit)){
            $obj = $obj->page($page, $limit);
        }

        if(!empty($orderField) && !empty($orderType)){
            $obj = $obj->order($orderField, $orderType);
        }

        $list = $obj->select();
        if($list){
            $return = [];
            foreach($list as $item){
                $return[] = $this->getInfo($item['address_id']);
            }
            return $return;
        }
        return [];
    }



    /*
    * 获取订单状态详情
    * */
    public function getInfo($id){
        $cacheKey = $this->cacheKey.$id;
        $redisModel = new RedisModel();
        $info = $redisModel->get($cacheKey);
        if($info){
            $info =  json_decode($info,true);
        }else{
            $info = Db::name('user_addresses')->find($id);
            $redisModel->set($cacheKey,json_encode($info,256),$this->cacheTime);
        }
        return $info;
    }




    /*
 * 获取地址
 * */
    public function isUserAddress($uid,$address_id){
        if(empty($address_id) || empty($uid)) return false;

        $where=[];
        $where[] = ["uid","=",$uid];
        $where[] = ["address_id","=",$address_id];
        $where[] = ["address_status","=",1];
        return $this->getTotal($where);
    }


    /*
   * 添加地址
   * */
    public function addOne($data){
        return Db::name('user_addresses')->save($data);
    }



    /*
     * 获取用户地址数量
     * */
    public function getUserAddressNum($uid){
        $where=[];
        $where['uid'] = $uid;
        $where['address_status'] = 1;
        return $this->getTotal($where);
    }


    /*
     * 获取用户地址列表
     * */
    public function getUserAddressList($uid){
        $where=[];
        $where['uid'] = $uid;
        $where['address_status'] = 1;
        return $this->getList($where,null,null,"address_id","DESC");
    }



    /*
     * 获取用户默认地址
     * */
    public function getUserDefaultAddressInfo($uid){
        $where=[];
        $where['uid'] = $uid;
        $where['address_status'] = 1;
        $where['is_default'] = 1;
        $address = Db::name('user_addresses')->field("address_id")->where($where)->find();
        return $this->getInfo($address['address_id']);
    }


    /*
     * 删除用户默认地址
     * */

    public function cancelUserDefaultAddress($uid){
        $where=[];
        $where['uid'] = $uid;
        $where['address_status'] = 1;
        $where['is_default'] = 1;
        return $this->updateAll($where,['is_default'=>0]);
    }


    /*
   * 绑定用户地址为默认地址
   * */

    public function bindUserDefaultAddress($uid,$address_id){
        $this->cancelUserDefaultAddress($uid);
        $where=[];
        $where['uid'] = $uid;
        $where['address_status'] = 1;
        $where['address_id'] = $address_id;
        return $this->updateAll($where,['is_default'=>1]);
    }




    /*
     * 批量删除地址
     * */
    public function delUserAddress($uid,$ids){
        $where=[];
        $where[] = ["uid","=",$uid];
        $where[] = ["address_id","IN",$ids];
        return Db::name('user_addresses')->where($where)->delete();
    }



    /*
  * 更新条件更新订单
  * */
    public function updateAll($where,$data){
        return Db::name('user_addresses')->where($where)->update($data);
    }

}